package com.weixing.mall.provider.controller.rpc;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.api.model.dto.LoginLogDto;
import com.weixing.mall.provider.api.model.dto.UserDto;
import com.weixing.mall.provider.api.model.dto.UserUpdateDto;
import com.weixing.mall.provider.api.model.vo.*;
import com.weixing.mall.provider.model.domain.CustomerAppdepot;
import com.weixing.mall.provider.model.domain.LoginLog;
import com.weixing.mall.provider.model.domain.Role;
import com.weixing.mall.provider.model.domain.User;
import com.weixing.mall.provider.model.enums.sys.DataGroupEnum;
import com.weixing.mall.provider.model.enums.sys.SysFlag;
import com.weixing.mall.provider.service.ICustomerAppdepotService;
import com.weixing.mall.provider.service.ILoginLogService;
import com.weixing.mall.provider.service.IRoleUserService;
import com.weixing.mall.provider.service.IUserService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wx
 * @Date 2019/12/13 9:53
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/user")
public class SysUserRpcController extends BaseController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ILoginLogService loginLogService;
    @Autowired
    private IRoleUserService roleUserService;
    @Autowired
    private ICustomerAppdepotService customerAppdepotService;

    @GetMapping("getRoleById")
    public Result<List<RoleVo>> getRoleById(Long id) {
        return ResultUtil.success(userService.getRoleById(id));
    }


    @GetMapping("loadUserByUserName")
    public Result<SysUserAuthVo> loadUserByUserName(String userName) {
        User user = userService.findByLoginName(userName);
        SysUserAuthVo sysUserVo = null;
        DataGroupEnum dataGroup = DataGroupEnum.INSENSITIVITY;
        if (null != user) {
            sysUserVo = new SysUserAuthVo();
            BeanUtils.copyProperties(user, sysUserVo);
            List<Role> roleUsers = roleUserService.findByUserId(user.getId());
            StringBuilder sb = new StringBuilder();
            if (CollUtil.isNotEmpty(roleUsers)) {
                int size = roleUsers.size();
                for (int i = 0; i < size; i++) {
                    Role role = roleUsers.get(i);
                    sb.append(role.getRoleCode());
                    if (DataGroupEnum.SENSITIVE.getValue().equals(role.getDataGroup())) {
                        dataGroup = DataGroupEnum.SENSITIVE;
                    }
                    if (i < size - 1) {
                        sb.append(",");
                    }
                }
            }
            sysUserVo.setDataGroup(dataGroup.getValue());
            sysUserVo.setRoles(sb.toString());
        }

        return ResultUtil.success(sysUserVo);
    }

    @GetMapping("loadUserByMobile")
    public Result<SysUserAuthVo> loadUserByMobile(String mobile) {
        System.out.println("rpc请求到达");
        User user = userService.findByMobile(mobile);
        SysUserAuthVo sysUserVo = new SysUserAuthVo();
        BeanUtils.copyProperties(user, sysUserVo);
        System.out.println(JSON.toJSONString(sysUserVo));
        return ResultUtil.success(sysUserVo);
    }


    @GetMapping("userInfo")
    public Result<UserInfoVo> userInfo(Long id) {
        // vue权限相关
        List<VueAuthorizeBean> vueAuthorizeBeans = userService.loadVueAuthorize(id, SysFlag.SYS);
        User sysUser = userService.getById(id);
        UserInfoVo infoVo = new UserInfoVo();
        infoVo.setAvator(sysUser.getAvator());
        infoVo.setUserName(sysUser.getLoginName());
        infoVo.setVuePaths(vueAuthorizeBeans);
        return ResultUtil.success(infoVo);
    }


    @PostMapping("updateUser")
    public Result<UserInfoVo> updateUser(@RequestBody UserUpdateDto sysUser) {
        User user = new User();
        BeanUtils.copyProperties(sysUser, user);
        userService.updateById(user);
        return ResultUtil.success();
    }


    @PostMapping("saveLoginLog")
    public Result<UserInfoVo> saveLoginLog(@RequestBody LoginLogDto sysLoginLog) {
        LoginLog loginLog = new LoginLog();
        BeanUtils.copyProperties(sysLoginLog, loginLog);
        loginLogService.save(loginLog);
        return ResultUtil.success();
    }

    @GetMapping("getUserListBydeptId")
    public Result<List<SysUserAuthVo>> getUserListBydeptId(Long id) {
        List<SysUserAuthVo> voList = Lists.newArrayList();
        List<User> list = userService.list(new QueryWrapper<User>().eq("dept_id", id));
        for (User entity : list) {
            SysUserAuthVo vo = new SysUserAuthVo();
            BeanUtils.copyProperties(entity, vo);
            voList.add(vo);
        }
        return ResultUtil.success(voList);
    }


    @GetMapping("loadKefu")
    public Result loadKefu() {
        List<KefuVo> kefulist = userService.loadKefu();
        return ResultUtil.success(kefulist);
    }

    @GetMapping("loadKefuByUserName")
    public Result loadKefuByUserName(String loginName) {
        KefuVo kefulist = userService.loadKefuByLoginName(loginName);
        return ResultUtil.success(kefulist);
    }

    @GetMapping("loadKefuById")
    public Result loadKefuById(Long id) {
        KefuVo kefu = userService.loadKefuById(id);
        return ResultUtil.success(kefu);
    }


    @GetMapping("getById")
    public Result<SysUserVo> getById(Long id) {
        User user = userService.getById(id);
        SysUserVo vo = null;
        if (null != user) {
            vo = new SysUserVo();
            BeanUtils.copyProperties(user, vo);
        }
        return ResultUtil.success(vo);
    }

    @PostMapping("createUser")
    public Result<SysUserVo> createUser(@RequestBody UserDto userDto) {
        Long userId = userService.createUser(userDto);
        User byId = userService.getById(userId);
        SysUserVo sysUserVo = new SysUserVo();
        BeanUtils.copyProperties(byId, sysUserVo);
        return ResultUtil.success(sysUserVo);
    }


    @PostMapping("selectByLoginName")
    public Result<SysUserVo> selectByLoginName(String loginName) {
        System.out.println("loginName:" + loginName );
        User user = userService.findByLoginName(loginName);
        SysUserVo sysUserVo = null;
        if (null != user) {
            sysUserVo = new SysUserVo();
            BeanUtils.copyProperties(user, sysUserVo);
        }

        return ResultUtil.success(sysUserVo);
    }


    @PostMapping("selectKefuList")
    public Result<IPage<KefuVo>> selectKefuList(long currentPage, long pageSize) {
        IPage<KefuVo> page = userService.loadKefuOfPage(initMpPage());

        return ResultUtil.success(page);
    }

    /**
     * 检查客服与APP仓库人员的绑定关系
     * @param customerId 客服ID，appDepotId 仓库登录人员Id
     * @return
     */
    @GetMapping("checkBind")
    public Result checkBind(Long customerId,Long appDepotId) {
        CustomerAppdepot customerAppdepot1 = customerAppdepotService.getOne(new QueryWrapper<CustomerAppdepot>().eq("app_depot_id", appDepotId));
        CustomerAppdepot customerAppdepot = customerAppdepotService.getOne(new QueryWrapper<CustomerAppdepot>().eq("customer_id", customerId).eq("app_depot_id", appDepotId));
        if(null==customerAppdepot1){
            return  ResultUtil.error("未绑定客服，无法操作");
        }
        if(null==customerAppdepot){

            return  ResultUtil.error("订单扫描人和客服绑定关系不对");
        }else{
            return  ResultUtil.success();
        }

    }

}
